//
//  ReadSpeech.m
//  AVSpeechSynthesizerDemo
//
//  Created by 德哥 on 16/3/28.
//  Copyright © 2016年 德哥. All rights reserved.
//

#import "XReadSpeechUtility.h"
#import <AVFoundation/AVFoundation.h>
#import "Macro.h"

@implementation ReadSpeech

+ (void)readSpeechForText:(NSString *)Text  Rate:(float)rate DiscardOld:(BOOL)dsiscardOld
{
    static AVSpeechSynthesizer *synthesizer;
    static dispatch_once_t once;
    static float utteranceRatre = 0.08;
    if ([[[UIDevice currentDevice ]systemVersion] intValue]>=9) {
        utteranceRatre = 0.5;
    }
    
    dispatch_once(&once, ^{
        synthesizer = [[AVSpeechSynthesizer alloc] init];
    });
    
    if (dsiscardOld) {
        [synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryWord];
    }
    
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:Text];
    //设置语言类别（不能被识别，返回值为nil）
    AVSpeechSynthesisVoice *voiceType = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-TW"];
    utterance.voice = voiceType;
    //设置语速快慢
    utterance.rate = (rate <= 0 ? 1 : rate) * utteranceRatre;;
    //语音合成器会生成音频
    [synthesizer speakUtterance:utterance];
    //    utteranceRatre -= 0.1;
}
@end
